<?php
namespace Mobile\Controller;
use Think\Controller;
class DzpController extends Controller 
{
    //大转盘
    public function dzpp()
    {   
        //解密
        $de = decrypt($_COOKIE['name'], 'cj'); 
        //获取用户信息
        $info = M('user_cj_user')->where(array('id'=>$de['id']))->find();
        $this->assign('info',$info);

        //检测今天有没有抽过奖
        $jt = strtotime(date('Y-m-d'));
        $jt_end = strtotime(date('Y-m-d'.'23:59:59'));
        $condition['cj_time'] = array('between',"$jt,$jt_end");
        $is_cj = M('user_cj_info')->where(array('phone_id'=>$info['id']))->where($condition)->find();
        $this->assign('is_cj',$is_cj);

        //参与人数
        $rcount = M('user_cj_user')->count();
        $this->assign('rcount',$rcount);
        //次数
        $ccount = M('user_cj_info')->count();
        $this->assign('ccount',$ccount);

        $_SESSION["sj_mobile"] = 6488;
        $this->display();
    }

    public function zj()
    {
        //print_r($_POST);
        //prize表示奖项内容，v表示中奖几率(若数组中七个奖项的v的总和为100，如果v的值为1，则代表中奖几率为1%，依此类推)
        $prize_arr = M('user_cj_lw')->select();
        foreach ($prize_arr as $k=>$v) {
            $arr[$v['id']] = $v['v'];

        }

        $prize_id = $this->getRand($arr); //根据概率获取奖项id 
        foreach($prize_arr as $k=>$v){ //获取前端奖项位置
            if($v['id'] == $prize_id){
             $prize_site = $k;
             break;
            }
        }
        $res = $prize_arr[$prize_id - 1]; //中奖项 

        $data['prize_name'] = $res['lw_name'];
        $data['prize_site'] = $prize_site;//前端奖项从-1开始
        $data['prize_id'] = $prize_id;
        //echo json_encode($data);

        //入库
        $array['num'] = $data['prize_id'];
        $array['phone_id'] = decrypt($_COOKIE['name'], 'cj')['id'];
        $array['cj_time'] = time();
        $res = M('user_cj_info')->add($array);
        if ($res) {

             $data['error'] = 1;
             echo json_encode($data);
         }else {
             $data['error'] = 0;
             echo json_encode($data);
         }
    }

    //九宫格抽奖概率
    public function getRand($proArr) {
        $data = '';
        $proSum = array_sum($proArr); //概率数组的总概率精度 

        foreach ($proArr as $k => $v) { //概率数组循环
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $v) {
                $data = $k;
                break;
            } else {
                $proSum -= $v;
            }
        }
        unset($proArr);

        return $data;
    }

    //提交手机显示
    public function tj()
    {
        if (IS_POST) {
            $data['phone'] = I('post.phone');
            $send_to = I('post.send_to');
            $data['time'] = time();

             //手机
             $preg_js = "/^1[34578][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }

            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '验证码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //入库 设置允许抽奖状态
            //检测是否第一次
            $find_phone = M('user_cj_user')->where(array('phone'=>$data['phone']))->find();
            if (!$find_phone) {
                //第一次
                $res = M('user_cj_user')->add($data);
                if ($res) {
                      //写COOKIE 记住 加密
                      $array = array('phone'=>$data['phone'],'id'=>$res);
                      $name = encrypt($array, 'cj'); 
                      setcookie('name',$name,time()+6209600);

                     $arr = array(
                        'error' =>1,
                        'info'=>"验证成功"
                        );
                      echo json_encode($arr);
                 }else {
                     $arr = array(
                            'error' =>0,
                            'info'=>"验证失败"
                            );
                    echo json_encode($arr);
                 }
            }else {
                $array = array('phone'=>$find_phone['phone'],'id'=>$find_phone['id']);
                $name = encrypt($array, 'cj'); 
                setcookie('name',$name,time()+6209600);

                //多次
                $arr = array(
                    'error' =>1,
                    'info'=>"验证成功"
                    );
                echo json_encode($arr);
                
            }

        }else {
            $this->display();
        }
    }

    //中奖列表
    public function lw()
    {   
        //解密
        $de = decrypt($_COOKIE['name'], 'cj'); 
        //取出当前用户的中奖记录
        $lws = M('user_cj_info')->where(array('phone_id'=>$de['id']))->order('id desc')->limit(5)->select();
        $this->assign('lws',$lws);

        $this->display();
    }








}